import os
import cv2
from pathlib import Path
import shutil

# UNZIP_FILES = False
MV_VIDEO_FILES = False
# RESAMPLE_FPS = True
# RESAMPLE_RATE = False


if __name__ == '__main__':

    path_root = Path("/disk2/yihua/LRW")


    if MV_VIDEO_FILES:
        path_data = path_root.joinpath("lipread_mp4")

        v_ids = sorted(path_data.glob("*"))
        print(f"Have {len(v_ids)} dir files, \n {v_ids[:10]}")

        # for each word
        for i, ipath_id in enumerate(v_ids):
            id_name = ipath_id.stem

            dir_video = path_root.joinpath("lipread_mp4", id_name)
            v_dir_videos = sorted(dir_video.glob("**/*.mp4"))

            print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:10]}")
            
            # for each video
            for ipath_videos in v_dir_videos:
                ivideo_name = ipath_videos.stem
                itrain_test = ipath_videos.parent.name

                # copy video
                ipath_video_target = path_root.joinpath("processing", id_name, itrain_test, ivideo_name, "video/video_orignal.mp4")
                
                if not ipath_video_target.exists():
                    ipath_video_target.parent.mkdir(parents=True, exist_ok=True)
                    shutil.copy(src=str(ipath_videos), dst=str(ipath_video_target))

                    #print(f"copy {ipath_videos} --> {ipath_video_target}")

                # copy audio 
                ipath_audio_source = path_root.joinpath("lipread_wav", id_name, itrain_test, ivideo_name+".wav")
                ipath_audio_target = path_root.joinpath("processing", id_name, itrain_test, ivideo_name, "audio/audio_orignal.wav")

                if not ipath_audio_target.exists():
                    ipath_audio_target.parent.mkdir(parents=True, exist_ok=True)
                    shutil.copy(src=str(ipath_audio_source), dst=str(ipath_audio_target))

                    #print(f"copy {ipath_audio_source} --> {ipath_audio_target}")


    # if RESAMPLE_FPS:
    #     path_data = path_root.joinpath("processing")

    #     v_ids = sorted(path_data.glob("*"))
    #     print(f"Have {len(v_ids)} dir files, \n {v_ids[:10]}")

    #     # for each word
    #     for i, ipath_id in enumerate(v_ids):
    #         id_name = ipath_id.stem

    #         dir_video = path_data.joinpath(id_name)
    #         v_dir_videos = sorted(dir_video.glob("**/*.mp4"))

    #         print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:10]}")

    #         # for each video
    #         for ipath_videos in v_dir_videos:
    #             ivideo_name = ipath_videos.stem
    #             itrain_test = ipath_videos.parent.name

    #             # copy video
    #             ipath_video_target = ipath_videos.parent.joinpath("video_25fps.mp4")

    #             cap = cv2.VideoCapture(str(ipath_videos))
    #             nframe = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #             nfps = int(cap.get(cv2.CAP_PROP_FPS))
    #             w, h = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                
    #             print(f"FPS = {nfps}, length = {nframe}, (w,h)=({w}, {h})")

    #             if not ipath_video_target.exists():
    #                 #ipath_video_target.parent.mkdir(parents=True, exist_ok=True)
    #                 #shutil.copy(src=str(ipath_videos), dst=str(ipath_video_target))
    #                 pass
    #                 #print(f"convert {ipath_videos} --> {ipath_video_target}")

              




                




